/*
* created by Administrator on 2018/11/16
* 好的代码像粥一样,是得用时间和精力熬的。
*/
var slideItem = document.getElementsByClassName('list--item__slide')[0];
var slideFlag = false;
var originPosX = 0;
var currentX = 0;
var distance = 0;
var openingSlider = null;
slideItem.addEventListener('touchstart', function (event) {
    var e = event || window.event;
    if(openingSlider) {
        if(openingSlider === this) {

        }else{
            openingSlider.style.left = 0 +'px';
            slideFlag = true;
            originPosX = e.changedTouches[0].clientX;
        }
    }
});
slideItem.addEventListener('touchmove', function (event) {
    var e = event || window.event;
    if (slideFlag) {
        distance = currentX - originPosX;
        currentX = e.changedTouches[0].clientX;
        if(distance < 0){
            if(distance <= -70){
                this.style.left = ( -70 / distance ) * (distance + 70) -70 + 'px';
            }else{
                this.style.left = currentX - originPosX + 'px';
            }
        }
    }
});
slideItem.addEventListener('touchend', function () {
    slideFlag = false;
    if(distance < -35){
        this.style.left = -70 + 'px';
        this.style.transition = 'left .2s ease-in-out';
        openingSlider = this;
    }else{
        this.style.left = 0 + 'px';
        this.style.transition = 'left .2s ease-in-out';
    }
});
slideItem.addEventListener('tap', function () {
    if(openingSlider === this) {
        this.style.left = 0 +'px';
    }else{
        openingSlider.style.left = 0 +'px';
        this.addEventListener('transitionEnd', function () {
            alert('跳转到this详情...');
        })
    }
});

/*;(function (window, document, undefined) {
    function getEle(selector) {
        return document.querySelector(selector);
    }
    function bindEv(ele, type, eventHandler) {
        if (ele.addEventListener){
            ele.addEventListener(type, function (event) {
                var e = event || window.event;
                eventHandler(e);
            });
        } else if (ele.addEventListener){
            ele.attach('on' + type, function (event) {
                var e = event || window.event;
                eventHandler(e);
            });
        } else {
            ele['on' + type] = eventHandler;
        }
    }
    function detectIsSliderItem(_e, targetCls) {
        var targetEle = _e.target || _e.srcElement;
        return targetEle.parentNode.className === targetCls ? targetEle.parentNode : null;
    }
    function setStyle(ele, attrName, attrValue) {
        if(attrName === 'left') {
            ele.style.left = parseInt(attrValue) + 'px';
        }else {
            ele.style[attrName] = attrValue;
        }
    }
    function shrink(ele, terminal, transition) {
        setStyle(ele, 'left', terminal);
        setStyle(ele, 'transition', transition)
    }
    function slider(delegationName, eventTarget) {
        // 事件的委托对象
        var delegateParent = typeof delegationName === 'object' ? delegationName : getEle(delegationName);
        // 触发事件的目标对象的selector
        var _target = eventTarget;
        // 是否可滑动
        var slideFlag = false;
        // 鼠标初始位置
        var originPosX = 0;
        // 鼠标move时不断更新的位置
        var currentPosX = 0;
        // 鼠标移动前后x轴方向的距离
        var distance = 0;
        // 展开状态的列表项
        var openingSlider = null;
        function slideStart(event) {
            var e = event || window.event;
            var target = detectIsSliderItem(event, _target);
            if (target) {
                if (openingSlider) {
                    if (openingSlider === this) {
                        // 收起 this/openingSlider
                        shrink(target, 0, 'all .2s ease-in-out');
                    } else {
                        // 收起 openingSlider
                        shrink(openingSlider, 0, 'all .2s ease-in-out');
                        // 展开 this
                        slideFlag = true;
                        originPosX = e.changedTouches[0].clientX;
                        currentPosX = e.changedTouches[0].clientX;
                    }
                } else {
                    // 展开 this
                    slideFlag = true;
                    originPosX = e.changedTouches[0].clientX;
                    currentPosX = e.changedTouches[0].clientX;
                }
            }
        }
        function slideMove(event) {
            var e = event || window.event;
            var target = detectIsSliderItem(event, _target);
            if (slideFlag) {
                currentPosX = e.changedTouches[0].clientX;
                distance = currentPosX - originPosX;
                if(distance < 0){
                    if(distance <= -70){
                        target.style.left = ( -70 / distance ) * (distance + 70) -70 + 'px';
                    }else{
                        target.style.left = distance + 'px';
                    }
                }
            }
        }
        function slideEnd() {
            var target = detectIsSliderItem(event, _target);
            if (distance > -35) {
                shrink(target, 0, 'all .2s ease-in-out');
            } else {
                shrink(target, -70, 'all .2s ease-in-out');
                openingSlider = target;
            }
        }
        function tapSlide() {
            console.log(123);
        }
        bindEv(delegateParent, 'touchstart', slideStart);
        bindEv(delegateParent, 'touchmove', slideMove);
        bindEv(delegateParent, 'touchend', slideEnd);
        bindEv(delegateParent, 'tap', tapSlide);
    }
    window.slider = slider;
})(window, document);
slider('.list', 'list--item__slide');*/
